

let fs = require("fs")

let jf = require("json-format")

let rwpath = "F:/B09 v1.4.3606"

let lrPath = rwpath + "/Data/Royalty/Defs/AbilityDefs/Abilities.xml";
let lrPath_cn = "E:/1/tmp/rw_hq/DefInjected/AbilityDef/Abilities.xml";

let str = fs.readFileSync(lrPath).toString();
let cnStr = fs.readFileSync(lrPath_cn).toString();
{/* <AnimaTreeLinking.label></AnimaTreeLinking.label>
<AnimaTreeLinking.description></AnimaTreeLinking.description> */}
let cns = {}

let matchCn = cnStr.match(/<[a-zA-Z0-9]+\.[a-zA-Z0-9]+>[^<]+<\/[a-zA-Z0-9]+\.[a-zA-Z0-9]+>/g)
matchCn.forEach(v => {
    if (/<([a-zA-Z0-9]+)\.([a-zA-Z0-9]+)>([^<]+)<\/[a-zA-Z0-9]+\.[a-zA-Z0-9]+>/.test(v)) {
        var key = RegExp.$1;
        var prop = RegExp.$2;
        var val = RegExp.$3;
        if (!cns[key]) {
            cns[key] = {}
        }
        cns[key][prop] = val
    }
});






// <AbilityDef ParentName="PsycastBase">
//     <defName>Stun</defName>
//     <label>stun</label>
//     <description>Momentarily disrupt motor function in the target's brain, preventing any movement.</description>
//     <level>1</level>
//     <iconPath>UI/Abilities/Stun</iconPath>
//     <writeCombatLog>True</writeCombatLog>
//     <hotKey>Misc3</hotKey>
//     <statBases>
//       <Ability_EntropyGain>12</Ability_EntropyGain>
//       <Ability_PsyfocusCost>0.01</Ability_PsyfocusCost>
//       <Ability_Duration>3</Ability_Duration>
//     </statBases>
//     <verbProperties>
//       <warmupTime>0.25</warmupTime>
//       <range>24.9</range>
//       <targetParams>
//         <canTargetAnimals>True</canTargetAnimals>
//       </targetParams>
//     </verbProperties>
//     <comps>
//       <li Class = "CompProperties_AbilityEffectWithDuration">
//         <compClass>CompAbilityEffect_Stun</compClass>
//         <durationMultiplier>PsychicSensitivity</durationMultiplier>
//         <psychic>True</psychic>
//         <goodwillImpact>-15</goodwillImpact>
//       </li>
//       <li Class="CompProperties_AbilityFleckOnTarget">
//         <fleckDef>PsycastPsychicEffect</fleckDef>
//       </li>
//     </comps>
//   </AbilityDef>


let matchs = str.match(/<AbilityDef(?:(?!<\/AbilityDef>)[\w\W])+<\/AbilityDef>/ig)

// humanPrimary  team  ob  yohai
/*
    zhongdi wakuang xunshou shejiao
    chuzi zhongdi xunshou
    yiliao  zhili
    jianzao zhizuo  yishu 
    
    no sleep [^ farm wakuang yisheng? xunshou shejiao jianzao]  has cook zhizuo yishu zhili
*/
let arr = matchs.map(v => {
    let data = {}
    let sts = ["label","level","Ability_EntropyGain","Ability_PsyfocusCost", "Ability_Duration","Ability_EffectRadius"
    ,"warmupTime","range","canTargetSelf","canTargetAnimals","canTargetItems","canTargetHumans","canTargetMechs",
    "canTargetBuildings","neverTargetHostileFaction","canTargetLocations","canTargetPawns","onlyTargetPrisonersOfColony",
    "canTargetPlants","onlyTargetAnimaTrees","neverTargetIncapacitated","onlyTargetColonists"];
    sts.forEach(s => {
        let reg = new RegExp(`<${s}>([^<]+)<`)
        if(reg.test(v)) {
            data[s] = RegExp.$1;
        }
    })
    let defName = (/<defName>([^<]+)</.exec(v)||[])[1];
    if (cns[defName]){
        data.cn = cns[defName]
    }
    return data
});
// arr = arr.filter(v=>Object.keys(v)>0)
arr = arr.sort((a,b)=>(a.level-0)-(b.level-0))

arr.sort(function (a, b) {
    return ((a.StuffPower_Armor_Sharp-0)||0.81) - ((b.StuffPower_Armor_Sharp-0)||0.81)
});


fs.writeFileSync("./ln.js", jf(arr))